import 'dart:math';
import 'package:flutter/material.dart';
import 'staking_screen.dart';
import 'securitization_screen.dart';
import 'package:hrcapp/app_routes.dart';

class WalletScreen extends StatelessWidget {
  const WalletScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF8F9FA),
      appBar: AppBar(
        title: const Text('我的钱包'),
        backgroundColor: Colors.white,
        foregroundColor: const Color(0xFF333333),
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, size: 16),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.settings_outlined),
            onPressed: () {
              // 设置按钮逻辑
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            // 总资产卡片
      Container(
        margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
        width: double.infinity,
        padding: const EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: const Color(0xFF1890FF),
          borderRadius: BorderRadius.circular(16),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              '总资产 (USDT)',
              style: TextStyle(
                fontSize: 16,
                color: Color(0xFFE6F7FF),
              ),
            ),
            const SizedBox(height: 10),
            const Text(
              '12,345.67',
              style: TextStyle(
                fontSize: 36,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
            const SizedBox(height: 10),
            const Text(
              '+2.5% (24h)',
              style: TextStyle(
                fontSize: 16,
                color: Color(0xFFE6F7FF),
              ),
            ),
            const SizedBox(height: 15),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () {
                    // 充值逻辑
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF40A9FF),
                    fixedSize: const Size(110, 30),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15),
                    ),
                  ),
                  child: const Text(
                    '充值',
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                ),
                const SizedBox(width: 15),
                ElevatedButton(
                  onPressed: () {
                    // 提现逻辑
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF69C0FF),
                    fixedSize: const Size(110, 30),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15),
                    ),
                  ),
                  child: const Text(
                    '提现',
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),

      const SizedBox(height: 20),

      // 快捷功能按钮区域
      Container(
        margin: const EdgeInsets.symmetric(horizontal: 20),
        padding: const EdgeInsets.symmetric(vertical: 20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.06),
              blurRadius: 12,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _buildQuickActionButton(
              icon: Icons.send_outlined,
              label: '转账',
              backgroundColor: const Color(0xFFE6F7FF),
              iconColor: const Color(0xFF1890FF),
              customIcon: CustomPaint(
                size: const Size(28, 28),
                painter: TransferIconPainter(const Color(0xFF1890FF)),
              ),
              onTap: () {
                _showTransferDialog(context);
              },
            ),
            _buildQuickActionButton(
              icon: Icons.qr_code_scanner_outlined,
              label: '收款',
              backgroundColor: const Color(0xFFF0FFF4),
              iconColor: const Color(0xFF52C41A),
              customIcon: CustomPaint(
                size: const Size(28, 28),
                painter: ReceiveIconPainter(const Color(0xFF52C41A)),
              ),
              onTap: () {
                _showReceiveDialog(context);
              },
            ),
            _buildQuickActionButton(
              icon: Icons.swap_horiz_outlined,
              label: '兑换',
              backgroundColor: const Color(0xFFFFF7E6),
              iconColor: const Color(0xFFFAAD14),
              customIcon: CustomPaint(
                size: const Size(28, 28),
                painter: ExchangeIconPainter(const Color(0xFFFAAD14)),
              ),
              onTap: () {
                _showExchangeDialog(context);
              },
            ),
            _buildQuickActionButton(
              icon: Icons.payment_outlined,
              label: '支付',
              backgroundColor: const Color(0xFFF0F5FF),
              iconColor: const Color(0xFF1890FF),
              customIcon: CustomPaint(
                size: const Size(28, 28),
                painter: PaymentIconPainter(const Color(0xFF1890FF)),
              ),
              onTap: () {
                Navigator.pushNamed(context, '/payment');
              },
            ),
          ],
        ),
      ),

      const SizedBox(height: 20),

      // 投资者中心入口
      Container(
        margin: const EdgeInsets.symmetric(horizontal: 20),
        child: GestureDetector(
          onTap: () {
            Navigator.pushNamed(context, '/investorCenter');
          },
          child: Container(
            padding: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.06),
                  blurRadius: 12,
                  offset: const Offset(0, 2),
                ),
              ],
            ),
            child: Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: const Color(0xFFF6FFED),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: const Icon(
                    Icons.trending_up,
                    color: Color(0xFF52C41A),
                    size: 24,
                  ),
                ),
                const SizedBox(width: 12),
                const Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '投资者中心',
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                          color: Color(0xFF262626),
                        ),
                      ),
                      SizedBox(height: 4),
                      Text(
                        '查看资产分析、获取投资建议',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0xFF8C8C8C),
                        ),
                      ),
                    ],
                  ),
                ),
                const Icon(
                  Icons.arrow_forward_ios,
                  size: 16,
                  color: Color(0xFFBFBFBF),
                ),
              ],
            ),
          ),
        ),
      ),

      const SizedBox(height: 20),

      // 理财模块区域
      Container(
        margin: const EdgeInsets.symmetric(horizontal: 20),
        padding: const EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(16),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.06),
              blurRadius: 12,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const Text(
                  '理财服务',
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF333333),
                  ),
                ),
                TextButton(
                  onPressed: () {
                    // 查看更多理财产品
                  },
                  child: const Text(
                    '查看全部',
                    style: TextStyle(
                      fontSize: 14,
                      color: Color(0xFF1890FF),
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 15),
            Row(
              children: [
                Expanded(
                  child: _buildFinanceCard(
                    title: '质押理财',
                    subtitle: '稳健收益',
                    rate: '8.5%',
                    period: '年化收益率',
                    backgroundColor: const Color(0xFFE6F7FF),
                    iconColor: const Color(0xFF1890FF),
                    icon: Icons.lock_outline,
                    onTap: () {
                      _showStakingDialog(context);
                    },
                  ),
                ),
                const SizedBox(width: 15),
                Expanded(
                  child: _buildFinanceCard(
                    title: '资产证券化',
                    subtitle: '资产上链',
                    rate: '12.8%',
                    period: '预期收益率',
                    backgroundColor: const Color(0xFFF6FFED),
                    iconColor: const Color(0xFF52C41A),
                    icon: Icons.account_balance,
                    onTap: () {
                      _showSecuritizationDialog(context);
                    },
                  ),
                ),
              ],
            ),
            const SizedBox(height: 15),
            Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: const Color(0xFFFFF7E6),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Row(
                children: [
                  Icon(
                    Icons.info_outline,
                    color: const Color(0xFFFAAD14),
                    size: 16,
                  ),
                  const SizedBox(width: 8),
                  Expanded(
                    child: Text(
                      '理财有风险，投资需谨慎。收益率仅供参考，实际收益以产品为准。',
                      style: TextStyle(
                        fontSize: 12,
                        color: const Color(0xFF666666),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),

      const SizedBox(height: 20),

            // 资产分布
      Container(
        margin: const EdgeInsets.symmetric(horizontal: 20),
        width: double.infinity,
        padding: const EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(16),
          boxShadow: const [
            BoxShadow(
              color: Color(0x1A000000),
              blurRadius: 20,
              offset: Offset(0, 4),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '资产分布',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                color: Color(0xFF333333),
              ),
            ),
            const SizedBox(height: 20),
            Row(
              children: [
                // 饼图
                Padding(
                  padding: const EdgeInsets.only(right: 15),
                  child: CustomPaint(
                    size: const Size(100, 100),
                    painter: PieChartPainter(),
                  ),
                ),
                // 图例
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildLegendItem(const Color(0xFF1890FF), '苏绣币 (40%)'),
                      _buildLegendItem(const Color(0xFF40A9FF), '古琴币 (30%)'),
                      _buildLegendItem(const Color(0xFF69C0FF), '木雕币 (20%)'),
                      _buildLegendItem(const Color(0xFF91CBFF), 'USDT (10%)'),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),

            const SizedBox(height: 20),

            // 资产列表
      Container(
        margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
        width: double.infinity,
        padding: const EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(16),
          boxShadow: const [
            BoxShadow(
              color: Color(0x1A000000),
              blurRadius: 20,
              offset: Offset(0, 4),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '资产列表',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                color: Color(0xFF333333),
              ),
            ),
            const SizedBox(height: 10),
            _buildAssetItem(
              '苏绣币',
              'HRC-SX',
              '5,678.90',
              '+2.7%',
              true,
              context,
            ),
            _buildAssetItem(
              '古琴币',
              'HRC-GQ',
              '3,456.78',
              '+5.3%',
              true,
              context,
            ),
            _buildAssetItem(
              '木雕币',
              'HRC-MD',
              '1,234.56',
              '-1.2%',
              false,
              context,
            ),
          ],
        ),
      ),
          ],
        ),
      ),
    );
  }

  Widget _buildLegendItem(Color color, String text) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 12, right: 20),
      child: Row(
        children: [
          Container(
            width: 12,
            height: 12,
            color: color,
          ),
          const SizedBox(width: 8),
          Text(
            text,
            style: const TextStyle(
              fontSize: 14,
              color: Color(0xFF666666),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildAssetItem(
    String name,
    String symbol,
    String balance,
    String change,
    bool isUp,
    BuildContext context,
  ) {
    return GestureDetector(
      onTap: () {
        // 导航到交易页面，并传递选中的资产
        Navigator.pushNamed(
          context, 
          AppRoutes.trade,
          arguments: {'selectedAsset': name},
        );
      },
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
        padding: const EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: const Color(0xFFF0F2F5),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  name,
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF333333),
                  ),
                ),
                Text(
                  symbol,
                  style: const TextStyle(
                    fontSize: 12,
                    color: Color(0xFF666666),
                  ),
                ),
              ],
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  balance,
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF333333),
                  ),
                ),
                Text(
                  change,
                  style: TextStyle(
                    fontSize: 12,
                    color: isUp ? const Color(0xFF52C41A) : const Color(0xFFF5222D),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  // 构建快捷功能按钮
  Widget _buildQuickActionButton({
    required IconData icon,
    required String label,
    required Color backgroundColor,
    required Color iconColor,
    required VoidCallback onTap,
    Widget? customIcon,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 60,
            height: 60,
            decoration: BoxDecoration(
              color: backgroundColor,
              shape: BoxShape.circle,
              boxShadow: [
                BoxShadow(
                  color: iconColor.withOpacity(0.15),
                  blurRadius: 8,
                  offset: const Offset(0, 2),
                ),
              ],
            ),
            child: customIcon ?? Icon(
              icon,
              color: iconColor,
              size: 28,
            ),
          ),
          const SizedBox(height: 8),
          Text(
            label,
            style: const TextStyle(
              fontSize: 12,
              color: Color(0xFF333333),
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  // 显示转账对话框
  void _showTransferDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('转账'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextField(
                decoration: const InputDecoration(
                  labelText: '收款地址',
                  hintText: '请输入收款地址或扫描二维码',
                  border: OutlineInputBorder(),
                ),
              ),
              const SizedBox(height: 16),
              TextField(
                decoration: const InputDecoration(
                  labelText: '转账金额',
                  hintText: '请输入转账金额',
                  border: OutlineInputBorder(),
                ),
                keyboardType: TextInputType.number,
              ),
              const SizedBox(height: 16),
              DropdownButtonFormField<String>(
                decoration: const InputDecoration(
                  labelText: '选择币种',
                  border: OutlineInputBorder(),
                ),
                items: ['苏绣币', '古琴币', '木雕币', 'USDT']
                    .map((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
                onChanged: (String? newValue) {
                  // 处理币种选择
                },
              ),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消'),
            ),
            ElevatedButton(
              onPressed: () {
                // 执行转账逻辑
                Navigator.of(context).pop();
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('转账功能开发中...')),
                );
              },
              child: const Text('确认转账'),
            ),
          ],
        );
      },
    );
  }

  // 显示收款对话框
  void _showReceiveDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('收款'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                width: 200,
                height: 200,
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.grey),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: const Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.qr_code,
                        size: 100,
                        color: Colors.grey,
                      ),
                      SizedBox(height: 8),
                      Text(
                        '收款二维码',
                        style: TextStyle(color: Colors.grey),
                      ),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 16),
              const Text(
                '钱包地址:',
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 8),
              Container(
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.grey[100],
                  borderRadius: BorderRadius.circular(8),
                ),
                child: const Text(
                  '0x1234567890abcdef1234567890abcdef12345678',
                  style: TextStyle(fontSize: 12),
                ),
              ),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('关闭'),
            ),
            ElevatedButton(
              onPressed: () {
                // 复制地址逻辑
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('地址已复制到剪贴板')),
                );
              },
              child: const Text('复制地址'),
            ),
          ],
        );
      },
    );
  }

  // 构建理财卡片
  Widget _buildFinanceCard({
    required String title,
    required String subtitle,
    required String rate,
    required String period,
    required Color backgroundColor,
    required Color iconColor,
    required IconData icon,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: BorderRadius.circular(12),
          border: Border.all(
            color: iconColor.withOpacity(0.2),
            width: 1,
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Icon(
                  icon,
                  color: iconColor,
                  size: 20,
                ),
                const SizedBox(width: 8),
                Expanded(
                  child: Text(
                    title,
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                      color: const Color(0xFF333333),
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 8),
            Text(
              subtitle,
              style: TextStyle(
                fontSize: 12,
                color: const Color(0xFF666666),
              ),
            ),
            const SizedBox(height: 12),
            Text(
              rate,
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                color: iconColor,
              ),
            ),
            Text(
              period,
              style: TextStyle(
                fontSize: 10,
                color: const Color(0xFF999999),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 显示理财选项
  void _showFinanceOptions(BuildContext context) {
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (BuildContext context) {
        return Container(
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(20),
              topRight: Radius.circular(20),
            ),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                margin: const EdgeInsets.only(top: 8),
                width: 40,
                height: 4,
                decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
              const Padding(
                padding: EdgeInsets.all(20),
                child: Text(
                  '理财服务',
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF333333),
                  ),
                ),
              ),
              ListTile(
                leading: Container(
                  width: 40,
                  height: 40,
                  decoration: BoxDecoration(
                    color: const Color(0xFFE6F7FF),
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: const Icon(
                    Icons.lock_outline,
                    color: Color(0xFF1890FF),
                    size: 20,
                  ),
                ),
                title: const Text('质押理财'),
                subtitle: const Text('稳健收益，年化8.5%'),
                trailing: const Icon(Icons.arrow_forward_ios, size: 16),
                onTap: () {
                  Navigator.pop(context);
                  _showStakingDialog(context);
                },
              ),
              ListTile(
                leading: Container(
                  width: 40,
                  height: 40,
                  decoration: BoxDecoration(
                    color: const Color(0xFFF6FFED),
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: const Icon(
                    Icons.account_balance,
                    color: Color(0xFF52C41A),
                    size: 20,
                  ),
                ),
                title: const Text('资产证券化'),
                subtitle: const Text('资产上链，预期收益12.8%'),
                trailing: const Icon(Icons.arrow_forward_ios, size: 16),
                onTap: () {
                  Navigator.pop(context);
                  _showSecuritizationDialog(context);
                },
              ),
              const SizedBox(height: 20),
            ],
          ),
        );
      },
    );
  }

  // 显示质押理财对话框
  void _showStakingDialog(BuildContext context) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => const StakingScreen(),
      ),
    );
  }

  // 跳转到资产证券化页面
  void _showSecuritizationDialog(BuildContext context) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => const SecuritizationScreen(),
      ),
    );
  }

  // 显示兑换对话框
  void _showExchangeDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('币种兑换'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              DropdownButtonFormField<String>(
                decoration: const InputDecoration(
                  labelText: '兑换币种',
                  border: OutlineInputBorder(),
                ),
                items: ['苏绣币', '古琴币', '木雕币', 'USDT']
                    .map((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
                onChanged: (String? newValue) {
                  // 处理币种选择
                },
              ),
              const SizedBox(height: 16),
              TextField(
                decoration: const InputDecoration(
                  labelText: '兑换数量',
                  hintText: '请输入兑换数量',
                  border: OutlineInputBorder(),
                ),
                keyboardType: TextInputType.number,
              ),
              const SizedBox(height: 16),
              DropdownButtonFormField<String>(
                decoration: const InputDecoration(
                  labelText: '目标币种',
                  border: OutlineInputBorder(),
                ),
                items: ['苏绣币', '古琴币', '木雕币', 'USDT']
                    .map((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
                onChanged: (String? newValue) {
                  // 处理目标币种选择
                },
              ),
              const SizedBox(height: 16),
              Container(
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.blue[50],
                  borderRadius: BorderRadius.circular(8),
                ),
                child: const Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('预计获得:'),
                    Text(
                      '0.00',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        color: Colors.blue,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消'),
            ),
            ElevatedButton(
              onPressed: () {
                // 执行兑换逻辑
                Navigator.of(context).pop();
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('兑换功能开发中...')),
                );
              },
              child: const Text('确认兑换'),
            ),
          ],
        );
      },
    );
  }
}

// 饼图绘制器
class PieChartPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width / 2;

    // 绘制背景圆
    final backgroundPaint = Paint()..color = const Color(0xFFF0F2F5);
    canvas.drawCircle(center, radius, backgroundPaint);

    // 绘制各个扇形
    final paint1 = Paint()..color = const Color(0xFF1890FF);
    final paint2 = Paint()..color = const Color(0xFF40A9FF);
    final paint3 = Paint()..color = const Color(0xFF69C0FF);
    final paint4 = Paint()..color = const Color(0xFF91CBFF);

    // 40% 的扇形
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      -pi / 2,
      2 * pi * 0.4,
      true,
      paint1,
    );

    // 30% 的扇形
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      -pi / 2 + 2 * pi * 0.4,
      2 * pi * 0.3,
      true,
      paint2,
    );

    // 20% 的扇形
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      -pi / 2 + 2 * pi * 0.7,
      2 * pi * 0.2,
      true,
      paint3,
    );

    // 10% 的扇形
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      -pi / 2 + 2 * pi * 0.9,
      2 * pi * 0.1,
      true,
      paint4,
    );
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

// 转账图标绘制器
class TransferIconPainter extends CustomPainter {
  final Color color;
  
  TransferIconPainter(this.color);
  
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 2.0
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round;
    
    final center = Offset(size.width / 2, size.height / 2);
    
    // 绘制箭头线条
    canvas.drawLine(
      Offset(center.dx - 8, center.dy),
      Offset(center.dx + 8, center.dy),
      paint,
    );
    
    // 绘制箭头头部
    final arrowPath = Path();
    arrowPath.moveTo(center.dx + 5, center.dy - 3);
    arrowPath.lineTo(center.dx + 8, center.dy);
    arrowPath.lineTo(center.dx + 5, center.dy + 3);
    
    canvas.drawPath(arrowPath, paint);
  }
  
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

// 收款图标绘制器
class ReceiveIconPainter extends CustomPainter {
  final Color color;
  
  ReceiveIconPainter(this.color);
  
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 2.0
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round;
    
    final center = Offset(size.width / 2, size.height / 2);
    
    // 绘制箭头线条
    canvas.drawLine(
      Offset(center.dx + 8, center.dy),
      Offset(center.dx - 8, center.dy),
      paint,
    );
    
    // 绘制箭头头部
    final arrowPath = Path();
    arrowPath.moveTo(center.dx - 5, center.dy - 3);
    arrowPath.lineTo(center.dx - 8, center.dy);
    arrowPath.lineTo(center.dx - 5, center.dy + 3);
    
    canvas.drawPath(arrowPath, paint);
  }
  
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

// 兑换图标绘制器
class ExchangeIconPainter extends CustomPainter {
  final Color color;
  
  ExchangeIconPainter(this.color);
  
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 1.5
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round;
    
    final center = Offset(size.width / 2, size.height / 2);
    
    // 绘制上方箭头线条
    canvas.drawLine(
      Offset(center.dx - 8, center.dy - 3),
      Offset(center.dx + 8, center.dy - 3),
      paint,
    );
    
    // 绘制下方箭头线条
    canvas.drawLine(
      Offset(center.dx - 8, center.dy + 3),
      Offset(center.dx + 8, center.dy + 3),
      paint,
    );
    
    // 绘制右侧箭头头部
    final rightArrowPath = Path();
    rightArrowPath.moveTo(center.dx + 5, center.dy - 6);
    rightArrowPath.lineTo(center.dx + 8, center.dy - 3);
    rightArrowPath.lineTo(center.dx + 5, center.dy);
    canvas.drawPath(rightArrowPath, paint);
    
    // 绘制左侧箭头头部
    final leftArrowPath = Path();
    leftArrowPath.moveTo(center.dx - 5, center.dy);
    leftArrowPath.lineTo(center.dx - 8, center.dy + 3);
    leftArrowPath.lineTo(center.dx - 5, center.dy + 6);
    canvas.drawPath(leftArrowPath, paint);
  }
  
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

// 理财图标绘制器
class FinanceIconPainter extends CustomPainter {
  final Color color;
  
  FinanceIconPainter(this.color);
  
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 2.0
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round;
    
    final center = Offset(size.width / 2, size.height / 2);
    
    // 绘制百分比符号的圆圈
    canvas.drawCircle(
      Offset(center.dx - 6, center.dy - 4),
      2,
      Paint()..color = color..style = PaintingStyle.fill,
    );
    
    canvas.drawCircle(
      Offset(center.dx + 6, center.dy + 4),
      2,
      Paint()..color = color..style = PaintingStyle.fill,
    );
    
    // 绘制百分比符号的斜线
    canvas.drawLine(
      Offset(center.dx - 8, center.dy + 6),
      Offset(center.dx + 8, center.dy - 6),
      paint,
    );
    
    // 绘制上升箭头
    final arrowPath = Path();
    arrowPath.moveTo(center.dx - 2, center.dy + 8);
    arrowPath.lineTo(center.dx + 2, center.dy + 4);
    arrowPath.lineTo(center.dx + 6, center.dy + 8);
    arrowPath.moveTo(center.dx + 2, center.dy + 4);
    arrowPath.lineTo(center.dx + 2, center.dy + 10);
    
    canvas.drawPath(arrowPath, paint);
  }
  
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

// 支付图标绘制器
class PaymentIconPainter extends CustomPainter {
  final Color color;
  
  PaymentIconPainter(this.color);
  
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 2.0
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round;
    
    final center = Offset(size.width / 2, size.height / 2);
    
    // 绘制信用卡外框
    final cardRect = RRect.fromRectAndRadius(
      Rect.fromCenter(
        center: center,
        width: size.width * 0.8,
        height: size.height * 0.6,
      ),
      const Radius.circular(3),
    );
    canvas.drawRRect(cardRect, paint);
    
    // 绘制磁条
    final stripRect = Rect.fromLTWH(
      center.dx - size.width * 0.35,
      center.dy - size.height * 0.1,
      size.width * 0.7,
      size.height * 0.08,
    );
    canvas.drawRect(
      stripRect,
      Paint()..color = color..style = PaintingStyle.fill,
    );
    
    // 绘制芯片
    final chipRect = RRect.fromRectAndRadius(
      Rect.fromCenter(
        center: Offset(center.dx - size.width * 0.15, center.dy + size.height * 0.1),
        width: size.width * 0.15,
        height: size.height * 0.12,
      ),
      const Radius.circular(1),
    );
    canvas.drawRRect(chipRect, paint);
  }
  
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}